//
//  MYGHomeMoreCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/6.
//

import UIKit

class MYGToolsCollectionViewCell: UICollectionViewCell {
    // MARK: - Property
    var model: MYGHomeToolsItem? {
        didSet {
            guard let model = model else {
                return
            }
            
            imageView.image = UIImage(named: model.image)
            nameLabel.text = model.name
            editButton.isSelected = !model.isHome
        }
    }
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(editButton)
        contentView.addSubview(nameLabel)
        
        imageView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(SCALEWIDTH(20))
            make.size.equalTo(CGSize(width: SCALEWIDTH(35), height: SCALEWIDTH(35)))
        }
        
        editButton.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.top).offset(-7)
            make.right.equalTo(imageView.snp.right).offset(7)
            make.width.height.equalTo(SCALEWIDTH(12.5))
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    // MARK: - lazy load
    private lazy var imageView: UIImageView = {
        let imgV = UIImageView()
        return imgV
    }()
    
    private(set) lazy var editButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "MYG_home_delete"), for: .normal)
        btn.setImage(UIImage(named: "MYG_home_add"), for: .selected)
        btn.isHidden = true
        
        btn.addTarget(self, action: #selector(changeSort), for: .touchUpInside)
        return btn
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(51, 51, 51, 1)
        return label
    }()
}

extension MYGToolsCollectionViewCell {
    @objc
    private func changeSort() {
        model?.changeAction?()
    }
}
